package cn.yunhe.servlet;

import cn.yunhe.dao.IUserAccount;
import cn.yunhe.entity.userentity.UserAccount;
import cn.yunhe.util.SqlSessionFactoryUtil;
import org.apache.ibatis.session.SqlSession;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * 王羊
 */
@WebServlet("/UserAccountServletDXDL")
public class UserAccountServletDXDL extends HttpServlet {
    SqlSession sqlSession = null;
    IUserAccount iuserAccount = null;

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        PrintWriter out = response.getWriter();
        //获取输入手机号
        String d = request.getParameter("tel");
        sqlSession = SqlSessionFactoryUtil.getSqlSession();
        iuserAccount = sqlSession.getMapper(IUserAccount.class);
        UserAccount userAccount = iuserAccount.duanxindenglu(d);
        HttpSession session = request.getSession();
        if (userAccount == null) {
            out.print("<script>alert('该用户不存在');location.href='register.html';</script>");
        } else {

            //获取输入的短信验证码
            String yan = request.getParameter("yan");
            //获取储存的短信验证码
            String yy = (String) request.getSession().getAttribute("yan");
            if (yan.equals(yy)) {
                session.setAttribute("uid",userAccount.getUid());
                out.print("<script>alert('登录成功');location.href='index.html';</script>");
            } else {
                out.print("<script>alert('验证码错误');</script>");
                //输入不正确 跳转至登录页面
                out.print("<script>location.href='login.html';</script>");
            }
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }


}